package com.smart.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * Created by khan on 15-2-14.
 */
@Repository
public class CartDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public     boolean createCart(int userid){
        long timestamp = Long.valueOf(System.currentTimeMillis())*1000*1000+userid;

        String sql="insert into cart(id,finish,userid) values(?,FALSE ,? )";
        int count = 0 ;
        try {
        count = jdbcTemplate.update(sql,new Object[]{timestamp,userid});
            System.out.print(count);
        }
      catch (Exception E){
          E.printStackTrace();
      }
        System.out.print(count);
        if (count == 1){

            return  true;
        }else {
          return false;
       }
    }

    public long getExistCartUnFinishedCart(int userid){
        String sql = "select id from cart where finish=? and userid = ?";
        long id= 0;
        try {
        id = jdbcTemplate.queryForLong(sql,new Object[]{false,userid});
        }catch (Exception E){
            E.printStackTrace();
        }
        return  id;
    }


}
